//
//  ViewController.m
//  MJ-1
//
//  Created by mark-work on 2018/3/21.
//  Copyright © 2018年 mark-work. All rights reserved.
//

#import "ViewController.h"
#import <AVOSCloud/AVOSCloud.h>

@interface ViewController ()<UIWebViewDelegate>
@property (nonatomic, weak) UIWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
        
    UIWebView *webView = [[UIWebView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    webView.delegate = self;
    webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webView];
    self.webView = webView;
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    

    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self getData];
//    AVQuery *query = [AVQuery queryWithClassName:@"URL"];
//    [query getObjectInBackgroundWithId:@"5ab30646ac502e57c937fa95" block:^(AVObject *object, NSError *error) {
//        // object 就是 id 为 558e20cbe4b060308e3eb36c 的 Todo 对象实例
//    }];
}


- (void)getData{
    AVObject *todo =[AVObject objectWithClassName:@"URL" objectId:@"5ab30a769f545444f0f3e282"];
    //    [todo setObject:@(YES) forKey: @"on"];
    //    [todo saveInBackground];
    [todo fetchInBackgroundWithBlock:^(AVObject *avObject, NSError *error) {
        NSString *urlStr = avObject[@"URLString"];// 读取 title
        NSNumber *on = avObject[@"on"]; // 读取 content
        
        NSURL * url ;
        if ([on boolValue]) {
            url = [NSURL URLWithString:urlStr];
            [[UIApplication sharedApplication] setStatusBarHidden:NO];
        }else{
            [[UIApplication sharedApplication] setStatusBarHidden:YES];

            url = [NSURL URLWithString:@"Html/index.html" relativeToURL:[[NSBundle mainBundle] bundleURL]];
        }
                NSURLRequest * request = [NSURLRequest requestWithURL:url];
                [self.webView loadRequest:request];
    }];
}



- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSLog(@"------%@",request.URL);
    
    if ([request.URL.absoluteString containsString:@"alipays"]) {
        [[UIApplication sharedApplication] openURL:request.URL];
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
